﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dominion
{
    public class Card
    {
        public static Card TestTreasureCard()
        {
            Card treasure = new Card(name: "Silver", 
                                     types: new CardType[] {CardType.Treasure}, 
                                     costs: new Cost[]     {new Cost(name: "Coin", amount: 3)});
            return treasure;
        }
        public static Card TestVictoryCard()
        {
            Card victory = new Card(name: "Duchy", 
                                    types: new CardType[] {CardType.Victory}, 
                                    costs: new Cost[]     {new Cost(name: "Coin", amount: 5)});
            return victory;
        }
        public static Card TestActionCard()
        {
            Card action = new Card(name: "Militia", 
                                   types: new CardType[] {CardType.Action, CardType.Attack}, 
                                   costs: new Cost[]     {new Cost(name: "Coin", amount: 4)});
            return action;
        }
        
        public List<CardType> Types           {get; protected set;}
        public string         Name            {get; protected set;}
        public string         RuleText        {get; protected set;}
        public string         FlavorText      {get; protected set;}
        public Dictionary<Currency, int> Cost {get; protected set;}
        public string Image                   {get; protected set;}
        
        public Card(string name, CardType[] types, Cost[] costs, string ruletext = null, string flavortext = null, string image = null)
        {
            Types      = new List<CardType>();
            Name       = string.Empty;
            RuleText   = string.Empty;
            FlavorText = string.Empty;
            Cost       = new Dictionary<Currency,int>();
            Image      = string.Empty;
            
            Name = name;
            Types.AddRange(types);
            foreach (Cost cost in costs)
            {
                if (Cost.ContainsKey(cost.Currency))
                {
                    Cost[cost.Currency] += cost.Amount;
                }
                else 
                { 
                    Cost.Add(cost.Currency, cost.Amount);
                }
            }
            RuleText   = ruletext   ?? string.Empty;
            FlavorText = flavortext ?? string.Empty;
            Image      = image      ?? string.Empty;
        }

        public string CostText
        {
            get
            {
                string s = string.Empty;
                foreach (KeyValuePair<Currency, int> kvp in Cost)
                {
                    if (s != string.Empty) s += ", ";
                    s += kvp.Value.ToString();
                    s += " ";
                    s += (1 == kvp.Value) ? kvp.Key.Name : kvp.Key.PluralName;
                }
                return s;
            }
            protected set {;}
        }

        public void Purchase()
        {
            throw new System.NotImplementedException();
        }

        public void Gain()
        {
            throw new System.NotImplementedException();
        }

        public void Play()
        {
            throw new System.NotImplementedException();
        }

        public void Discard()
        {
            throw new System.NotImplementedException();
        }

        public void Trash()
        {
            throw new System.NotImplementedException();
        }

        public void CleanUp()
        {
            throw new System.NotImplementedException();
        }
    }

    public enum CardType
    {
        Treasure,
        Victory,
        Kingdom,
        Base,
        Curse,
        Action,
        Reaction,
        Duration,
        Attack,
    }

    public class TokenType
    {
        public String Name {get; protected set;}
    }

    public class CardStack
    {
        public CardStack(){}
        public CardStack(Card card, uint? count)
        {
            Card  = card;
            Count = count;
        }
    
        public Card  Card  {get; protected set;}
        public uint? Count {get; protected set;}

        public Card Pop()
        {
            if (!Count.HasValue) return Card; // a null value means unlimited
            if (0 == Count)      return null;
            Count -= 1;
            return Card;
        }

        public void Push()
        {
            if (Count.HasValue) Count += 1;
        }
    }
}
